﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace YunShop_WebAPI.BLL
{
    /// <summary>
    /// 公共接口-后端
    /// </summary>
    public class Common
    {
        /// <summary>
        /// AES解密
        /// </summary>
        /// <param name="decryptStr">密文</param>
        /// <param name="key">密钥</param>
        /// <returns></returns>
        public static string PosDecrypt(string decryptStr)
        {
            try
            {
                string key = "k&WX@)!%09!AC8WB";//商云Key
                byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);
                byte[] toEncryptArray = Convert.FromBase64String(decryptStr);
                RijndaelManaged rDel = new RijndaelManaged();
                rDel.Key = keyArray;
                rDel.Mode = CipherMode.ECB;
                rDel.Padding = PaddingMode.PKCS7;
                ICryptoTransform cTransform = rDel.CreateDecryptor();
                byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
                return UTF8Encoding.UTF8.GetString(resultArray);
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        /// <summary>
        /// base64转普通string
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string ConvertBase64StringToString(string data)
        {
            byte[] b = Convert.FromBase64String(data);
            return System.Text.Encoding.UTF8.GetString(b);
        }

        /// <summary>
        /// AES加密
        /// </summary>
        /// <param name="encryptStr">明文</param>
        /// <param name="key">密钥</param>
        /// <returns></returns>
        public static string PosEncrypt(string encryptStr)
        {
            string key = "k&WX@)!%09!AC8WB";//商云Key
            string sEncryptType = System.Configuration.ConfigurationManager.AppSettings["EncryptType"];
            if (sEncryptType == "C8") key = "k&WX@)!%05!8C8WB";//C8 Key
            byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);
            byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(encryptStr);
            RijndaelManaged rDel = new RijndaelManaged();
            rDel.Key = keyArray;
            rDel.Mode = CipherMode.ECB;
            rDel.Padding = PaddingMode.PKCS7;
            ICryptoTransform cTransform = rDel.CreateEncryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }

        /// <summary>
        /// 转成 Base64 形式的 System.String  
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string ConvertToBase64String(string data)
        {
            byte[] b = System.Text.Encoding.UTF8.GetBytes(data);
            //转成 Base64 形式的 System.String  
            return Convert.ToBase64String(b);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        public static string GetValidJson(string json)
        {
            try
            {
                json = CommonFun.ConvertToString(json);
                string myjson = "";
                if (json.Substring(0, 1) != "[")
                    myjson = "[" + json;
                else
                    myjson = json;
                if (json.Substring(json.Length - 1, 1) != "]")
                    myjson = myjson + "]";

                return myjson;
            }
            catch
            {
                return "[{}]";
            }
        }
    }
}
